﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TuMama;
using System.Collections;
using System.Threading;
using System.Globalization;

namespace WindowsApplication2
{
    public partial class NuevoJuego : Form
    {
        bool esp;
        String nombreArchivo;
        int secs;
        int tuScore = 0;
        private Match match;
        private System.Threading.Timer waga;
        private ArrayList ocultables = new ArrayList();
        private System.Threading.TimerCallback cb;

        public NuevoJuego()
        {

            InitializeComponent();
            this.match = Match.thismatch;
            this.match.ListaJugadoresActualizada += new EventHandler(match_ListaJugadoresActualizada);
            this.match.GameStarted += new EventHandler(match_GameStarted);
            this.match.QuizReceived += new EventHandler(match_QuizReceived);
            this.match.YouWin += new EventHandler(match_YouWin);
            this.match.YouLose += new EventHandler(match_YouLose);
        }

        void match_ListaJugadoresActualizada(object sender, EventArgs e)
        {
            ArrayList temp = match.ListaJugadores;
            listBox1.Items.Clear();
            foreach (Jugador x in temp)
            {
                listBox1.Items.Add(x.NombreJugador + " - " + x.Score);
            }
        }

        private void ProcessTimerEvent(object a)
        {
            Random ran = new Random();
            secs--;
            label1.Text = secs.ToString();
            if (secs <= 0)
            {
                waga.Dispose();
                MediaPlayer1.Ctlcontrols.stop();
                //    SendScore();
            }
            int num = ran.Next(ocultables.Count);
            if (ocultables.Count > 0)
            {
                ((RadioButton)ocultables[num]).Visible = false;
                ocultables.Remove((RadioButton)ocultables[num]);
            }
        }

        void match_GameStarted(object sender, EventArgs e)
        {
            this.Visible = true;
            this.Enabled = true;
            label7.Text = match.PlayerName;
            Mensaje nuevo = new Mensaje(null, Mensaje.tipoObjeto.NULL, Mensaje.message.ready);
            this.match.SendMessage(nuevo);

        }

        void match_YouLose(object sender, EventArgs e)
        {
            String texto = "";
            foreach (Jugador x in match.ListaJugadores)
            {
                if (esp)
                    texto += "Jugador: " + x.NombreJugador + " Puntuación: " + x.Score.ToString() + Environment.NewLine;
                else
                    texto += "Player: " + x.NombreJugador + " Score: " + x.Score.ToString() + Environment.NewLine;
            }
            if (esp)
                MessageBox.Show("YOU LOSE!!!!" + Environment.NewLine + Environment.NewLine + texto);
            else
                MessageBox.Show("PERDISTE!!!!" + Environment.NewLine + Environment.NewLine + texto);
            Application.Exit();
        }

        void match_YouWin(object sender, EventArgs e)
        {
            String texto = "";
            foreach (Jugador x in match.ListaJugadores)
            {
                if (esp)
                    texto += "Jugador: " + x.NombreJugador + " Puntuación: " + x.Score.ToString() + Environment.NewLine;
                else
                    texto += "Player: " + x.NombreJugador + " Score: " + x.Score.ToString() + Environment.NewLine;
            }
            if (esp)
                MessageBox.Show("TU MAMA!!! GANASTE!!!" + Environment.NewLine + Environment.NewLine + texto);
            else
                MessageBox.Show("TU MAMA!!! YOU WIN!!!" + Environment.NewLine + Environment.NewLine + texto);
            Application.Exit();
        }

        void match_QuizReceived(object sender, EventArgs e)
        {
            this.NuevoMatch();
        }


        private String GetNextTitulo()
        {
            Random waga = new Random();
            int num = waga.Next(match.Lista.ListaTitulos.Count);
            String ret = (String)match.Lista.ListaTitulos[num];
            match.Lista.ListaTitulos.RemoveAt(num);
            return ret;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.MaximizeBox = false;
            this.ResetRadios();
            this.Enabled = false;
            if (Thread.CurrentThread.CurrentUICulture.Equals(new CultureInfo("es-MX")))
                esp = true;
            else
                esp = false;
        }

        private void NuevoMatch()
        {
            if (waga != null) waga.Dispose();
            if (cb != null) cb = null;
            secs = 11;
            nombreArchivo = ByteMp3.ConvertirHaciaArchivo(match.Lista.Cancion.BytesCancion, "waga.mp3");
            ocultables = new ArrayList();
            foreach (RadioButton radio in groupBox1.Controls)
            {
                radio.Text = GetNextTitulo();
                if (radio.Text != this.match.Lista.Cancion.Titulo)
                    ocultables.Add(radio);
            }
            this.SetVisibleAll();
            this.ResetRadios();
            MediaPlayer1.URL = nombreArchivo;
            cb = new System.Threading.TimerCallback(ProcessTimerEvent);
            waga = new System.Threading.Timer(cb, null, 0, 1000);
        }

        private void SetVisibleAll()
        {
            foreach (RadioButton radio in groupBox1.Controls)
            {
                radio.Visible = true;
            }
        }


        private void StopAll()
        {
            if (waga != null) waga.Dispose();
            if (cb != null) cb = null;
            MediaPlayer1.Ctlcontrols.stop();
            foreach (RadioButton radio in groupBox1.Controls)
            {
                radio.Visible = false;
                //radio.Checked = false;
            }

        }

        private void ResetRadios()
        {
            foreach (RadioButton radio in groupBox1.Controls)
            {
                radio.Checked = false;
            }
        }


        private void SendScore()
        {
            label4.Text = tuScore.ToString();
            Jugador x = new Jugador();
            x.Score = tuScore;
            Mensaje nuevo = new Mensaje(x, Mensaje.tipoObjeto.Jugador, Mensaje.message.sendScore);
            this.match.SendMessage(nuevo);

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton1.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton1.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton2.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton2.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton3.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton3.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton4.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton4.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton5.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton5.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton6.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton6.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton7.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton7.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton8.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton8.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton9.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton9.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void radioButton10_CheckedChanged(object sender, EventArgs e)
        {
            StopAll();
            if (radioButton10.Text == match.Lista.Cancion.Titulo)
            {
                tuScore = secs;
                secs = 0;
            }
            else
            {
                secs = 0;
            }
            if (radioButton10.Checked)
                SendScore();
            this.ResetRadios();
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }




    }
}